var add,edit,setVal,reset;
require(['select2', 'date','layout','validator','uploadify'], function() {
	var userInfo = commonFun.execParentMethod("getUserInfo");								//当前用户信息
    var $formData = $('#formData'),$importStatLgtLtt = $("#import_statLgtLtt"),
    	$exportStatLgtLtt = $("#export_statLgtLtt"),$statLgtLttFile = $("#statLgtLttFile"),
    	$qLineId =$('#qLineId'),$importStatLgtLttDialog = $("#import_statLgtLtt_dialog"),
    	$qLineType = $("#qLineType"),$export_statLgtLtt_box = $('export_statLgtLtt_box');
    $formData.initForm();
    
	add = function(fn) {
		addOrUpdate("/stationInfo/addStationInfo.htm",fn);
	};
	
	edit = function(fn) {
		addOrUpdate("/stationInfo/uptStationInfo.htm",fn);
	};
	
	setVal = function (stnId){
		$.ajaxExt({
			url :"/stationInfo/getStationInfoByStnId.htm",
			data : {'stnId' : stnId}
		}).done(function(data){
			$formData.setValues(data);
		});
	};
	reset = function (){
		$formData.reset($formData);
		if(userInfo.corpId != '39708c61759b48748c5bd853ba64ded5'){
			$("input[name='corpId']").val(userInfo.corpId);
		}
	};
	
	function addOrUpdate(url,callBack){
		var result = $.validator($formData);
		// console.info($formData.getValues());
    	if(result){
    		$.ajaxExt({
    			url : url,
    			data : $formData.getValues()
    		}).done(function(){
    			callBack();
    		});
    	}
	}
	
	// 站点名和站点别名同步
	$("#stnName").blur(function(){
		$("#stnAlias").val($("#stnName").val());
	});
	
	//导入站点经纬度
	$importStatLgtLtt.on('click', function(){
		$qLineId.html("");
		$.ajaxExt({
			url : $.getProjectName() + "/stationInfo/getBaseLineInfoList.htm"
		}).done(function(data){
			var lineInfoList = data;
			var optionH = "";
			$.each(lineInfoList,function(n,lineInfo){
				optionH = optionH + "<option value='"+lineInfo.lineId+"'>"+lineInfo.lineName+"</option>";
			});
			$qLineId.append(optionH);
			try{
				$statLgtLttFile.uploadify('cancel', '*');
			}catch(e){
				
			}
			$importStatLgtLttDialog.dialog('open');
		});
	});

	// 文件上传插件初始化
	$statLgtLttFile.uploadify({
		swf : $.getProjectName() + "/componet/uploadify/uploadify.swf",
		uploader : $.getProjectName() + "/stationInfo/uploadStatLgtLttFile.htm",
		fileObjName : 'statLgtLttFile',
		fileDataName : 'statLgtLttFile',
		auto : true,
		multi: false,
		method : 'post',
		height : 28,
		width : 150,
		buttonText : '选择文件',
		fileTypeDesc : '只能上传是xls和xlsx文件',
		fileTypeExts : '*.xls;*.xlsx',
		buttonClass : 'uploadify-bnt',
		onUploadSuccess : function(file,data,response){
			if(response){
				msg = jQuery.parseJSON(data);
				if(msg && msg.msgType){ // 有错误信息
					if (msg.msgType && msg.msgType == '2') {
						$.messager.alert("警告", msg.warnMsg || "系统异常", "warning");
					}else if (msg.msgType && msg.msgType == '3') {
						$.messager.alert("错误", msg.errorMsg || "系统异常", "error");
					}
				}else{ // 没有错误信息
					$.messager.alert("提示","站点经纬度导入成功。","info");
					setTimeout(function(){
						$importStatLgtLttDialog.dialog('close');
					},0);
					reloadGridData();
				}
			}else{
				$.messager.alert("提示","站点经纬度导入失败。","info");
			}
		}
	});

	//站点经纬度模版下载
	$exportStatLgtLtt.on('click', function(){
		var result = $.validator($export_statLgtLtt_box);
		if(result){
			var url = $.getProjectName() + "/stationInfo/exportStatLgtLtt2Excel.htm?qLineId="+$qLineId.val()+"&qLineType="+$qLineType.val();
			$exportStatLgtLttForm.attr("action",url);
			$exportStatLgtLttForm.submit();
		}
	});
	
	if(userInfo.corpId != '39708c61759b48748c5bd853ba64ded5'){
    	$("#corpli").hide();
    	$("input[name='corpId']").val(userInfo.corpId);
    }
	
});